package com.material.factory.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.material.common.exception.ServiceException;
import com.material.common.utils.DateUtils;
import com.material.common.utils.bean.BeanUtil;
import com.material.factory.params.FactoryReceivingPlanAdd;
import com.material.factory.params.FactoryReceivingPlanModelAdd;
import com.material.factory.params.FactoryReceivingPlanParams;
import com.material.factory.params.FactoryReceivingPlanQuery;
import com.material.factory.vo.FactoryReceivingPlanDataVo;
import com.material.factory.vo.FactoryReceivingPlanListVo;
import com.material.factory.vo.FactoryReceivingPlanVo;
import com.material.factory.vo.FuelInfoVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.material.factory.mapper.FactoryReceivingPlanMapper;
import com.material.factory.domain.FactoryReceivingPlan;
import com.material.factory.service.IFactoryReceivingPlanService;

/**
 * 工厂收货计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-07
 */
@Service
public class FactoryReceivingPlanServiceImpl implements IFactoryReceivingPlanService
{
    @Autowired
    private FactoryReceivingPlanMapper factoryReceivingPlanMapper;

    /**
     * 查询工厂收货计划
     *
     * @param id 工厂收货计划主键
     * @return 工厂收货计划
     */
    @Override
    public FactoryReceivingPlan selectFactoryReceivingPlanById(Long id)
    {
        return factoryReceivingPlanMapper.selectFactoryReceivingPlanById(id);
    }

    /**
     * 查询工厂收货计划列表
     *
     * @param factoryReceivingPlan 工厂收货计划
     * @return 工厂收货计划
     */
    @Override
    public List<FactoryReceivingPlan> selectFactoryReceivingPlanList(FactoryReceivingPlan factoryReceivingPlan)
    {
        return factoryReceivingPlanMapper.selectFactoryReceivingPlanList(factoryReceivingPlan);
    }

    @Override
    public PageInfo<FactoryReceivingPlanDataVo> selectFactoryReceivingPlanDateList(FactoryReceivingPlanParams factoryReceivingPlan) {
        PageHelper.startPage(factoryReceivingPlan.getPageNum(),factoryReceivingPlan.getPageSize());
        List<FactoryReceivingPlanVo> factoryReceivingPlanVos = factoryReceivingPlanMapper.selectFactoryReceivingPlanDateList(factoryReceivingPlan);
        List<String> dateList = factoryReceivingPlanVos.stream().map(FactoryReceivingPlanVo::getReceivingDate).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(dateList)){
            return new PageInfo<FactoryReceivingPlanDataVo>(new ArrayList<>());
        }
        List<FactoryReceivingPlanDataVo> list = factoryReceivingPlanMapper.selectGroupList(factoryReceivingPlan.getFactoryId(),dateList);
        Map<Date, List<FactoryReceivingPlanDataVo>> dateMap = list.stream().collect(Collectors.groupingBy(FactoryReceivingPlanDataVo::getReceivingDate));
        List<FactoryReceivingPlanDataVo> dataList = new ArrayList<>();
        dateMap.forEach((date,plan)->{
            List<FuelInfoVo> fuelInfoVos = BeanUtil.copyProperties(plan, FuelInfoVo.class);
            FactoryReceivingPlanDataVo data = plan.get(0);
            FactoryReceivingPlanDataVo dataVo = new FactoryReceivingPlanDataVo();
            dataVo.setFuelList(fuelInfoVos);
            dataVo.setFactoryName(data.getFactoryName());
            dataVo.setFactoryId(data.getFactoryId());
            dataVo.setReceivingDate(date);
            dataList.add(dataVo);
        });
        Collections.sort(dataList, Comparator.comparing(FactoryReceivingPlanDataVo::getReceivingDate).reversed());
        PageInfo<FactoryReceivingPlanDataVo> pageInfo = new PageInfo<>(dataList);
        return pageInfo;
    }

    /**
     * 新增工厂收货计划
     *
     * @param factoryReceivingPlan 工厂收货计划
     * @return 结果
     */
    @Override
    public int insertFactoryReceivingPlan(FactoryReceivingPlan factoryReceivingPlan)
    {
        factoryReceivingPlan.setCreateTime(DateUtils.getNowDate());
        return factoryReceivingPlanMapper.insertFactoryReceivingPlan(factoryReceivingPlan);
    }

    /**
     * 修改工厂收货计划
     *
     * @param factoryReceivingPlan 工厂收货计划
     * @return 结果
     */
    @Override
    public int updateFactoryReceivingPlan(FactoryReceivingPlan factoryReceivingPlan)
    {
        return factoryReceivingPlanMapper.updateFactoryReceivingPlan(factoryReceivingPlan);
    }

    /**
     * 批量删除工厂收货计划
     *
     * @param ids 需要删除的工厂收货计划主键
     * @return 结果
     */
    @Override
    public int deleteFactoryReceivingPlanByIds(Long[] ids)
    {
        return factoryReceivingPlanMapper.deleteFactoryReceivingPlanByIds(ids);
    }

    /**
     * 删除工厂收货计划信息
     *
     * @param id 工厂收货计划主键
     * @return 结果
     */
    @Override
    public int deleteFactoryReceivingPlanById(Long id)
    {
        return factoryReceivingPlanMapper.deleteFactoryReceivingPlanById(id);
    }

    @Override
    public List<FactoryReceivingPlanListVo> selectFactoryReceivingPlanPageList(FactoryReceivingPlanQuery factoryReceivingPlan) {
        return factoryReceivingPlanMapper.selectFactoryReceivingPlanPageList(factoryReceivingPlan);
    }

    @Override
    public int batchInsertFactoryReceivingPlan(FactoryReceivingPlanAdd factoryReceivingPlan) {
        Long factoryId = factoryReceivingPlan.getFactoryId();
        Long fuelId = factoryReceivingPlan.getFuelId();
        List<FactoryReceivingPlanModelAdd> dates = factoryReceivingPlan.getDates();
        List<FactoryReceivingPlan> list = new ArrayList<>();
        dates.forEach(d->{
            Date date = d.getDate();
            Double amount = d.getAmount();
            FactoryReceivingPlan plan = new FactoryReceivingPlan();
            plan.setFactoryId(factoryId);
            plan.setFuelId(fuelId);
            plan.setReceivingDate(date);
            plan.setQuantity(amount);
            list.add(plan);
            FactoryReceivingPlan exist = factoryReceivingPlanMapper.selectFactoryReceivingPlan(factoryId, fuelId, DateUtils.dateTimeFormat(date, DateUtils.YYYY_MM_DD));
            if(exist != null){
                throw new ServiceException(DateUtils.dateTimeFormat(date, DateUtils.YYYY_MM_DD)+"收货的燃料已经存在，请勿重复添加");
            }
        });
        return factoryReceivingPlanMapper.batchInsertFactoryReceivingPlan(list);
    }
}
